package com.example.facemanager.util;

import com.example.facemanager.config.Memory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author huchenying
 * @Date 2022/8/3 11:03
 * @Description
 */
@Component
@Slf4j
public class PostRequestUtils {

    @Autowired
    RestTemplate restTemplate;

    public Object postRequest(String url, Map<String, Object> map) {
        try {
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            HashMap<String, String> var = new HashMap<>();
            var.put("access_token", Memory.getAccess_token());
            Object result = restTemplate.postForEntity(url, map, Object.class, var);
            log.info("请求成功 result -> {}", result);
            return result;
        } catch (Exception e) {
            log.error("请求异常 -> {}", e.getMessage());
        }
        return null;
    }
}
